/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;
import java.util.Calendar;
import java.text.*;

import Models.KhuVuc_Model;
import Models.Ban_Model;
import Models.ThongKe_Model;
import Models.DoanhNghiep_Model;
import Models.ThamSoHT_Model;

/**
 *
 * @author Venus
 */
public class ThongKe_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            String ngaygio = fm.format(cal.getTime());
            
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vKhuVuc = new Vector();
                vKhuVuc = kvm.LayDSKVTaoBan(langid);
                Ban_Model bm = new Ban_Model();
                Vector vBan = new Vector();
                vBan = bm.LayDSBan();
                request.setAttribute("DSKV", vKhuVuc);
                request.setAttribute("DSBan", vBan);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/statistic.jsp");
                rd.forward(request, response);
            } else if (action.equals("ShowDKLoc")) {
                String loaithongke = request.getParameter("loaithongke");
                SimpleDateFormat fm1 = new SimpleDateFormat("yyyy");
                String year = fm1.format(cal.getTime());
                request.setAttribute("loaithongke", loaithongke);
                request.setAttribute("year", year);
                RequestDispatcher rd = request.getRequestDispatcher("admin/showdkloc.jsp");
                rd.forward(request, response);
            } else if (action.equals("CheckDate")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                if (ngaybd.equals("") && ngaykt.equals("")) {
                    out.print("true");
                } else {
                    if (ThongKe_Model.CheckDate(ngaybd, ngaykt)) {
                        out.print("true");
                    } else {
                        out.print("false");
                    }
                }
            } else if (action.equals("Turnoverfood")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String manhomma = request.getParameter("manhomma");
                String tenmonan = request.getParameter("tenmonan");
                String sl = request.getParameter("soluong");
                String chkchart = request.getParameter("chkchart");
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                if (chkchart != null) {                    
                    RequestDispatcher rd = request.getRequestDispatcher("admin/turnfoodchart.jsp");
                    rd.forward(request, response);
                } else {
                    double doanhthu = ThongKe_Model.DoanhThuMA(ngaybd, ngaykt, manhomma, tenmonan, langid, sl);                    
                    request.setAttribute("ngaybd", ngaybd);
                    request.setAttribute("ngaykt", ngaykt);
                    request.setAttribute("manhomma", manhomma);
                    request.setAttribute("tenmonan", tenmonan);
                    request.setAttribute("sl", sl);
                    request.setAttribute("doanhthu", Double.toString(doanhthu));                    
                    Vector v = new Vector();
                    if (manhomma.equals("all") && tenmonan.equals("")) {
                        v = ThongKe_Model.Turnoverfoodgr(ngaybd, ngaykt, manhomma, tenmonan, langid, sl);
                        request.setAttribute("nma", v);
                        RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverfood1.jsp");
                        rd.forward(request, response);
                    } else {
                        v = ThongKe_Model.Turnoverfood(ngaybd, ngaykt, manhomma, tenmonan, langid, sl);
                        request.setAttribute("Report", v);
                        RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverfood.jsp");
                        rd.forward(request, response);
                    }
                }
            }
            else if (action.equals("Chartturnoverr")) {
                String year = request.getParameter("year");
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                request.setAttribute("year", year);
                RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverchar.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("chartline")) {
                String year = request.getParameter("year");
                String dsts = "turnover";
                String turnover = ThamSoHT_Model.TenThamSoHT(dsts, langid);
                String result = "[{"
                        + " \"label\": \"" + turnover + "\",  \"value\": [  ";
                String[] arr = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
                for (int i = 0; i < arr.length; i++) {
                    double tk = ThongKe_Model.ThongKeTheoThang(Integer.parseInt(arr[i]), Integer.parseInt(year));
                    result = result + tk;
                    if (i != (arr.length - 1)) {
                        result = result + " , ";
                    }
                }
                result = result + " ] } ] ";
                out.print(result);
            }
            else if (action.equals("Turnoveruser")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String tennv = request.getParameter("tennv");
                String sl = request.getParameter("soluong");
                Vector v1 = new Vector();
                Vector v2 = new Vector();
                Vector v3 = new Vector();
                v1 = ThongKe_Model.Turnoveruser1(ngaybd, ngaykt, tennv, langid, sl);
                v2 = ThongKe_Model.Turnoveruser2(ngaybd, ngaykt, tennv, langid, sl);
                v3 = ThongKe_Model.Turnoveruser3(ngaybd, ngaykt, tennv, langid, sl);
                //double doanhthu = ThongKe_Model.DoanhThuUser(ngaybd, ngaykt, tennv, langid, sl);
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("Report1", v1);
                request.setAttribute("Report2", v2);
                request.setAttribute("Report3", v3);
                request.setAttribute("sl", sl);
                request.setAttribute("ngaybd", ngaybd);
                request.setAttribute("ngaykt", ngaykt);
                request.setAttribute("tennv", tennv);
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                //request.setAttribute("doanhthu", Double.toString(doanhthu));
                request.setAttribute("ngaygio", ngaygio);
                RequestDispatcher rd = request.getRequestDispatcher("admin/turnoveruser.jsp");
                rd.forward(request, response);
            } else if (action.equals("Turnovercustomer")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String tenkh = request.getParameter("tenkh");
                String sl = request.getParameter("soluong");
                Vector v = new Vector();                
                v = ThongKe_Model.Turnovercustomer(ngaybd, ngaykt, tenkh, langid, sl);
                double doanhthu = ThongKe_Model.DoanhThuKH(ngaybd, ngaykt, tenkh, langid, sl);
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("Report", v);
                request.setAttribute("sl", sl);
                request.setAttribute("ngaybd", ngaybd);
                request.setAttribute("ngaykt", ngaykt);
                request.setAttribute("tenkh", tenkh);
                request.setAttribute("doanhthu", Double.toString(doanhthu));
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                RequestDispatcher rd = request.getRequestDispatcher("admin/turnovercustomer.jsp");
                rd.forward(request, response);
            } else if (action.equals("Turnoverarea")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String makhuvuc = request.getParameter("makhuvuc");
                String tenban = request.getParameter("tenban");
                String soluong = request.getParameter("soluong");
                String chkchart = request.getParameter("chkchart");
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                if (chkchart != null) {                    
                    RequestDispatcher rd = request.getRequestDispatcher("admin/turnareachart.jsp");
                    rd.forward(request, response);
                } else {                    
                    double doanhthu = ThongKe_Model.DoanhThuBan(ngaybd, ngaykt, makhuvuc, tenban, langid, soluong);
                    request.setAttribute("soluong", soluong);
                    request.setAttribute("ngaybd", ngaybd);
                    request.setAttribute("ngaykt", ngaykt);
                    request.setAttribute("makhuvuc", makhuvuc);
                    request.setAttribute("tenban", tenban);
                    request.setAttribute("doanhthu", Double.toString(doanhthu));                    
                    Vector v = new Vector();
                    if (makhuvuc.equals("all") && tenban.equals("")) {
                        v = ThongKe_Model.Turnoverareagr(ngaybd, ngaykt, makhuvuc, tenban, langid, soluong);
                        request.setAttribute("kv", v);
                        RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverarea1.jsp");
                        rd.forward(request, response);
                    } else {
                        v = ThongKe_Model.Turnoverarea(ngaybd, ngaykt, makhuvuc, tenban, langid, soluong);
                        request.setAttribute("Report", v);
                        RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverarea.jsp");
                        rd.forward(request, response);
                    }
                }
            } else if (action.equals("Turnoverbill")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String makhuvuc = request.getParameter("makhuvuc");
                String tenban = request.getParameter("tenban");
                String soluong = request.getParameter("soluong");
                Vector v = new Vector();
                v = ThongKe_Model.Turnoverbill(ngaybd, ngaykt, makhuvuc, tenban, langid, soluong);
                double doanhthu = ThongKe_Model.DoanhThuHD(ngaybd, ngaykt, makhuvuc, tenban, langid, soluong);
                request.setAttribute("Report", v);
                request.setAttribute("soluong", soluong);
                request.setAttribute("ngaybd", ngaybd);
                request.setAttribute("ngaykt", ngaykt);
                request.setAttribute("makhuvuc", makhuvuc);
                request.setAttribute("tenban", tenban);
                request.setAttribute("doanhthu", Double.toString(doanhthu));
                request.setAttribute("ngaygio", ngaygio);
                RequestDispatcher rd = request.getRequestDispatcher("admin/turnoverbill.jsp");
                rd.forward(request, response);
            } else if (action.equals("Topfood")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String soluong = request.getParameter("soluong");
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaybd", ngaybd);
                request.setAttribute("ngaykt", ngaykt);
                request.setAttribute("soluong", soluong);
                request.setAttribute("ngaygio", ngaygio);
                Vector v = new Vector();
                if (soluong.equals("")) {
                    v = ThongKe_Model.Topfoodgr(ngaybd, ngaykt, soluong, langid);
                    request.setAttribute("nma", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/topfood1.jsp");
                    rd.forward(request, response);
                } else {
                    v = ThongKe_Model.Topfood(ngaybd, ngaykt, soluong, langid);
                    request.setAttribute("Report", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/topfood.jsp");
                    rd.forward(request, response);
                }
            } else if (action.equals("Toptable")) {
                String ngaybd = request.getParameter("ngaybd");
                String ngaykt = request.getParameter("ngaykt");
                String soluong = request.getParameter("soluong");
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                request.setAttribute("ngaybd", ngaybd);
                request.setAttribute("ngaykt", ngaykt);
                request.setAttribute("soluong", soluong);
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                Vector v = new Vector();
                if (soluong.equals("")) {
                    v = ThongKe_Model.Toptablegr(ngaybd, ngaykt, soluong, langid);
                    request.setAttribute("kv", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/toptable1.jsp");
                    rd.forward(request, response);
                } else {
                    v = ThongKe_Model.Toptable(ngaybd, ngaykt, soluong, langid);
                    request.setAttribute("Report", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/toptable.jsp");
                    rd.forward(request, response);
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
